package com.lin.filegraph.utils.analyze;

import com.lin.filegraph.utils.compdg.ComponentGraph;
import com.lin.filegraph.utils.compdg.ComponentNode;

import java.io.File;

/**
 * @description:
 * @author: linhuaixu
 * @time: 2023/5/18 15:53
 */

public class ExcelToGraph {

    public static ComponentGraph transform(String excelPath, String projectPath) {
        System.out.println(excelPath);
        ExcelPoi ePoi = new ExcelPoi(excelPath);
        ComponentGraph initalCom;
        // if (ePoi.projectPath.equals(projectPath) ) {
        initalCom = new ComponentGraph(projectPath);
        for (String[] coms : ePoi.comList) {
            String comName;
            ComponentNode cn = null;
            boolean isDir = true;
            if (coms[2] != null) {
                comName = coms[2];// 组件名
                cn = initalCom.getComponentByName(comName);
                if (cn == null) {
                    cn = new ComponentNode(comName);
                }
                if (coms[3] != null && coms[3].equals("1")) {// 判断是否仅当前目录
                    isDir = false;
                }
                String language = ePoi.language;
                String comPath="";
                if (coms[0].equals("-1"))
                    comPath = projectPath;
                else {
                    if(coms[0].charAt(0)!='/')
                        coms[0]="/"+coms[0];
                    comPath = projectPath+coms[0];
                }
                if(!comPath.equals("")){
                    if (coms[1].equals("1")) {
                        if(new File(comPath).isFile())
                            cn.addFile(coms[0].substring(1));
                        else
                            System.out.println("输入有误："+coms[0]+"不是一个文件");
                    }else{
                        travelDir(new File(comPath), cn, projectPath, isDir, language);
                    }
                }
                if (cn != null&&cn.getAllFiles().size()>0) {
                    if(!initalCom.getAllComponents().contains(cn))
                        initalCom.addCompNode(cn);
                }
            }
            // } else {
            // initalCom = null;
            // System.out.println("文件输入的工程路径与实际不匹配：");
            // System.out.println("文件中路径："+ePoi.projectPath);
            // System.out.println("实际路径："+projectPath);
            // }
        }
        System.out.println("输入的架构如下：");
        for (ComponentNode cNode : initalCom.getAllComponents()) {
            System.out.println("组件名：" + cNode.getStrComponentName());
//			for (String dir : cNode.getAllFiles())
//				System.out.println("---" + dir);
        }
        return initalCom;
    }

    private static void travelDir(File d, ComponentNode cn, String projectPath, boolean isDir, String language) {
        if (d.isDirectory()) {
            for (File dir : d.listFiles()) {
                if (dir.isDirectory()) {
                    if (isDir) {
                        travelDir(dir, cn, projectPath, isDir, language);
                    }
                } else {
                    String filePath = dir.getAbsolutePath();
                    if (filePath.length() != projectPath.length())
                        filePath = filePath.substring(projectPath.length() + 1);
                    else
                        filePath = "-1";// 根目录
//					if (!cn.getAllFiles().contains(dirPath)) {
                    if (!visitEnum(cn, dir, filePath, language))
                        return;
                }
            }
        }
    }

    private static boolean visitEnum(ComponentNode cn, File dir, String filePath, String language) {
        // 判断编程语言
        if (language.equals("java") || language.equals("Java")) {
            for (Language.Java j : Language.Java.values()) {
                if (dir.getName().endsWith("." + j.toString())) {
                    cn.addFile(filePath);
                }
            }
        } else if (language.equals("c") || language.equals("C")) {
            for (Language.C c : Language.C.values()) {
                if (dir.getName().endsWith("." + c.toString())) {
                    cn.addFile(filePath);
                }
            }
        } else if (language.equals("c++") || language.equals("C++")) {
            for (Language.CPlus c : Language.CPlus.values()) {
                if (dir.getName().endsWith("." + c.toString())) {
                    cn.addFile(filePath);
                }
            }
        } else {
            System.out.println("输入的编程语言错误！");
            return false;
        }
        return true;
    }

}

